package com.rs.admin.sys.base.resolver;
import com.rs.admin.sys.base.annotation.PageParamsAnnotation;
import com.rs.common.entity.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 有@PageParamsAnnotation注解的方法参数，注入分页的参数
 * @author theodo
 * @email 36780272@qq.com
 * @date 2017-03-23 22:02
 */
@Component
public class PageParamsHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(PageParams.class) && parameter.hasParameterAnnotation(PageParamsAnnotation.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
        //分页对象，这里就直接采用硬编码了
        Object object = request.getAttribute("params", RequestAttributes.SCOPE_REQUEST);
        if(object == null){
            return null;
        }
        //获取分页参数
        PageParams pageParams = (PageParams)object;

        return pageParams;
    }
}
